﻿using AjaxControlToolkit;
using ASP;
using IPMS.AccessAuth;
using IPMS.BLL;
using IPMS.Sort;
using IPMS.Common;
using System;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using Resources;
using System.Web;
using SysOperationLayer.DBHandle;
using System.Data;
using SysComm;


public partial class OptProductUpdate : Page, IRequiresSessionState
{
    LogDB m_logdb = new LogDB();
    ProductDB m_productdb = new ProductDB();


    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!SysUtil.IsAdministratorOrNormalUserType())
        {
            base.Response.Redirect("ErrorPage/NotAllowed.aspx");
            return;
        }

        if (!IsPostBack)
        {
            ((Label)this.Master.FindControl("lblMstTitle")).Text = "| " + ISitemap.ProductManage.ToString() + " | " + ISitemap.ProductEdit.ToString();

            string model = Request["Model"].ToString();
            DataTable dt;
            dt = m_productdb.getProductById(model);
            if (dt != null)
            {
                txtModel.Text = m_productdb.GetValue(dt.Rows[0], "Model");
                txtModelName.Text = m_productdb.GetValue(dt.Rows[0], "ModelName");
                txtRevision.Text = m_productdb.GetValue(dt.Rows[0], "Revision");
                txtSpecification.Text = m_productdb.GetValue(dt.Rows[0], "Specification");
                txtRemarks1.Text = m_productdb.GetValue(dt.Rows[0], "Remark1");
                txtRemarks2.Text = m_productdb.GetValue(dt.Rows[0], "Remark2");
                txtRemarks3.Text = m_productdb.GetValue(dt.Rows[0], "Remark3");
                txtRemarks4.Text = m_productdb.GetValue(dt.Rows[0], "Remark4");
                txtRemarks5.Text = m_productdb.GetValue(dt.Rows[0], "Remark5");
                txtDate1.Text = m_productdb.GetValue(dt.Rows[0], "DateFld1");
                txtDate2.Text = m_productdb.GetValue(dt.Rows[0], "DateFld2");
                txtDate3.Text = m_productdb.GetValue(dt.Rows[0], "DateFld3");
                txtDate4.Text = m_productdb.GetValue(dt.Rows[0], "DateFld4");
                txtDate5.Text = m_productdb.GetValue(dt.Rows[0], "DateFld5");
                txtDeci1.Text = m_productdb.GetValue(dt.Rows[0], "DeciFld1");
                txtDeci2.Text = m_productdb.GetValue(dt.Rows[0], "DeciFld2");
            }
        }
    }


    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("OptProduct.aspx");
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        int ret;
        ret = m_productdb.updateProduct(txtModel.Text.Trim(),
                                    txtModelName.Text.Trim(),
                                    txtRevision.Text.Trim(),
                                    txtSpecification.Text.Trim(),
                                    txtRemarks1.Text.Trim(),
                                    txtRemarks2.Text.Trim(),
                                    txtRemarks3.Text.Trim(),
                                    txtRemarks4.Text.Trim(),
                                    txtRemarks5.Text.Trim(),
                                    txtDate1.Text.Trim(),
                                    txtDate2.Text.Trim(),
                                    txtDate3.Text.Trim(),
                                    txtDate4.Text.Trim(),
                                    txtDate5.Text.Trim(),
                                    txtDeci1.Text.Trim(),
                                    txtDeci2.Text.Trim());

        if (ret == 0)
        {
            m_logdb.writeSucLog(SysUtil.m_functiong_product, "update product  succeed!");
            Jscript.Alert("Update succeed!", this.Page);
        }
        else
        {
            m_logdb.writeFaileLog(SysUtil.m_functiong_product, "update product failed!");
            Jscript.Alert("Update failed!", this.Page);
            lblError.Text = m_productdb.ErrorMessage;
        }
    }
}
